package name.sraynal;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class FlagSelector {

	private File rscDir;
	private File outFile;

	public FlagSelector(String rscDir, String outFile) {
		this.rscDir = new File(rscDir);
		this.outFile = new File(outFile);
	}

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		if (args.length != 2) {
			System.out
					.println("Use is : FlagSelector [rscDir] [outFile]\nExecution aborted.");
			return;
		}
		String rscDir = args[0];
		String outFile = args[1];
		FlagSelector selector = new FlagSelector(rscDir, outFile);
		selector.execute();
	}

	private void execute() throws IOException {
		StringBuffer titlesBuffer = new StringBuffer();
		titlesBuffer.append("var titles = new Array(");
		Writer writer = new BufferedWriter(new FileWriter(outFile));
		writer.append("var flags = new Array(");

		// Create images
		String[] files = this.rscDir.list();
		for (int i = 0; i < files.length; i++) {
			String current = files[i];
			String title = current.replace(".svg.png", "").replace(".jpg", "")
					.replace('_', ' ').replace("px-Flag of ", "")
					.replace("0", "").replace("1", "").replace("2", "")
					.replace("3", "").replace("4", "").replace("5", "")
					.replace("6", "").replace("7", "").replace("8", "")
					.replace("9", "");
			writer.append("\"" + current + "\"");
			titlesBuffer.append("\"" + title + "\"");
			if (i < files.length - 1) {
				writer.append(", ");
				titlesBuffer.append(", ");
			}
		}

		writer.append(");\n\n");
		titlesBuffer.append(");");
		writer.append(titlesBuffer);
		writer.close();
	}
}
